package com.mew.slots.core.game.sgj.roller;

import com.mew.slots.core.common.roller.ili.xml.IliXml;
import com.mew.slots.core.game.sgj.room.SgjRoom;
import lombok.Data;
import lombok.ToString;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

@Data
@ToString
public class SgjIli extends IliXml {

    public ArrayList<Marryili> bouns;

    private static SgjIli instance;

    private SgjIli(){}

    static {
        instance = new SgjIli();
    }

    public static SgjIli getInstance(){
        return instance;
    }

    @Override
    public void initPathsRowColIlitype() {
        this.setRow(3);
        this.setCol(5);
        this.setIlitype(0);
        this.getPaths().add("external/roller/sgj/sgj_prob.xml");
        this.getPaths().add("external/roller/sgj/sgj_charge_prob.xml");
        this.setIlipath("external/roller/sgj/sgj_icon_lib.ili");
    }

    @Override
    public void initSpe() throws Exception {
        //地址
        String path = rootpath + File.separator + paths.get(0);

        SAXReader reader = new SAXReader();
        Document document = reader.read(new File(path));
        String xpath = "/Root/Special";///Item[@ID='1']
        Element node = (Element)document.selectSingleNode(xpath);

        ArrayList<Marryili> rollers = new ArrayList<>();
        //遍历子节点
        for(Iterator it = node.elementIterator(); it.hasNext();){
            //节点
            Element next = (Element) it.next();
            List attributes = next.attributes();
            Marryili roller = new Marryili();
            for (int i = 0; i < attributes.size(); i++) {
                Attribute a = (Attribute) attributes.get(i);
                String name = a.getName();
                String value = a.getValue();
                if ("ID".equals(name)){
                    roller.setId(Integer.parseInt(value));
                }else if ("max".equals(name)){
                    roller.setMax(Integer.parseInt(value));
                }else if ("min".equals(name)){
                    roller.setMin(Integer.parseInt(value));
                }else if ("Name".equals(name)){
                    roller.setName(value);
                }

                //权重
                int[] fruit = roller.getFruit();
                roller.setFruitMaryRand(new int[fruit.length]);
                int[] fruitMaryRand = roller.getFruitMaryRand();

                for(Iterator n = next.elementIterator();n.hasNext();){

                    //节点
                    Element cl = (Element) n.next();
                    String clName = cl.getName();

                    if ("AwardIcon".equals(clName)){

                        doAwardIcon(fruit, fruitMaryRand, cl);

                    }else if ("AwardProb".equals(clName)){

                        doAwardProb(roller, cl);

                    }else if ("Align".equals(clName)){

                        doAlign(roller, cl);

                    }

                }

            }

            if (null!=roller.getId()&&null!=roller.getMax()){
                //存到什么地方
                rollers.add(roller);
            }
        }

        this.bouns = rollers;
    }

    private void doAlign(Marryili roller, Element cl) {
        List clattributes = cl.attributes();
        int[] ps = new int[4];
        int temp = 0;
        for (int j = 0; j < clattributes.size(); j++) {
            Attribute ca = (Attribute) clattributes.get(j);
            String cname = ca.getName();
            String cvalue = ca.getValue();
            ps[j] = Integer.parseInt(cvalue);
        }

        int lose = 100 - Arrays.stream(ps).sum();
        int[] align = {lose,lose+ps[0],lose+ps[0]+ps[1],lose+ps[0]+ps[1]+ps[2],lose+ps[0]+ps[1]+ps[2]+ps[3]};
        roller.setSameFruit(align);
    }

    private void doAwardProb(Marryili roller, Element cl) {
        ArrayList<int[]> prodArr = new ArrayList<>();
        for (Iterator cln = cl.elementIterator();cln.hasNext();){
            Element ccl = (Element) cln.next();
            List clattributes = ccl.attributes();
            int[] icons = new int[2];
            for (int j = 0; j < clattributes.size(); j++) {
                Attribute ca = (Attribute) clattributes.get(j);
                String cname = ca.getName();
                String cvalue = ca.getValue();

                if ("Ok".equals(cname)){
                    icons[0] = Integer.parseInt(cvalue);
                }else if ("No".equals(cname)){
                    icons[1] = Integer.parseInt(cvalue);
                }
            }
            int stop = 100 - icons[0] - icons[1];
            int no0 = stop + icons[1];
            int win = no0 + icons[0];

            int[] prod = {stop,no0,win};
            prodArr.add(prod );
        }

        int[][] prods = new int[prodArr.size()+3][3];
        for (int k = 0; k < prodArr.size(); k++) {
            prods[k] = prodArr.get(k);
        }

        int[] end = {100,100,100};

        prods[prodArr.size()] = end;
        prods[prodArr.size()+1] = end;
        prods[prodArr.size()+2] = end;
        roller.setMaryRand(prods);
    }

    private void doAwardIcon(int[] fruit, int[] fruitMaryRand,  Element cl) {
        for (Iterator cln = cl.elementIterator();cln.hasNext();){
            Element ccl = (Element) cln.next();
            List clattributes = ccl.attributes();
            int[] icons = new int[2];
            for (int j = 0; j < clattributes.size(); j++) {
                Attribute ca = (Attribute) clattributes.get(j);
                String cname = ca.getName();
                String cvalue = ca.getValue();

                if ("name".equals(cname)){
                    icons[0] = Integer.parseInt(cvalue);
                }else if ("scaled".equals(cname)){
                    icons[1] = Integer.parseInt(cvalue);
                }
            }

            for (int k = 0; k < fruit.length; k++) {
                if (fruit[k]==icons[0]){
                    fruitMaryRand[k] = icons[1];
                }
            }
        }

    }

    //c转java
    @Override
    public void ctoj(ArrayList<Integer> line){

        for (int i = 0; i < line.size(); i++) {
            if (line.get(i)== SgjRoom._FREE){
                line.set(i,SgjRoom._SCATTER);
            }else if (line.get(i)== SgjRoom._SCATTER){
                line.set(i,SgjRoom._FREE);
            }
        }

    };


}
